package kr.ac.inha.vota;

import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.Toast;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
//import com.google.tts.TTS;

public class OpicExamActivity extends Activity implements OnInitListener {

//	private TTS myTts;
	private TextToSpeech tts;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_opic_exam);
        
//		myTts = new TTS(this, ttsInitListener, true);
        
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        checkIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivityForResult(checkIntent, 0);
        tts = new TextToSpeech(this, this);
    }
    
/*    
    private TTS.InitListener ttsInitListener = new TTS.InitListener() {
		
		public void onInit(int arg0) {
			// TODO Auto-generated method stub
			myTts.speak("Hello world. Hello Android", 0, null);
		}
	};
*/  
    
    public void onInit(int arg0) {
     // TODO Auto-generated method stub
     //String speech1 = "The controversial issue has been central infrastructure item of the national patient data repository";
    String speech1 = "Let me introduce yourself";
     tts.setLanguage(Locale.US);
     tts.speak(speech1, TextToSpeech.QUEUE_FLUSH, null);
    }
   
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_opic_exam, menu);
        return true;
    }
    
    public boolean onTouchEvent(MotionEvent event) {
		Intent intent;
		
		super.onTouchEvent(event);
		
		Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        
		Rect exam = new Rect();
		exam.set((int)(0.333*display.getWidth()),(int)(0.884*display.getHeight()),
				(int)(0.637*display.getWidth()),(int)(0.971*display.getHeight()));
		
		if(event.getAction() == MotionEvent.ACTION_DOWN) {
			if(exam.contains((int)event.getX(),(int)event.getY())) {
			Toast.makeText(OpicExamActivity.this, "Test End", Toast.LENGTH_SHORT).show();
			
			intent = new Intent(OpicExamActivity.this, OpicEndActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    		startActivity(intent);
    		finish();
			}
    		return false;
		}
		return true;
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if(event.getAction() == KeyEvent.ACTION_DOWN) {
    		if(keyCode == KeyEvent.KEYCODE_BACK) {
    			Intent intent = new Intent(OpicExamActivity.this, OpicMainActivity.class);
    			intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        		startActivity(intent);
        		finish();
        		
        		return false;
    		}
    	}
    	
    	// TODO Auto-generated method stub
    	return super.onKeyDown(keyCode, event);
    }
}